package com.springboot.system.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.springboot.system.dao.DepartmentDao;
import com.springboot.system.domain.auth.Department;
import com.springboot.common.algorithm.DepartmentAlgorithm;
import com.springboot.common.exception.SystemException;
import com.springboot.common.utils.BeanUtils;
import com.springboot.core.SysManager;

/**
 * 部门业务层实现类
 * 
 * @ClassName: DepartmentService
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 252956
 * @date 2021年1月7日 上午9:54:22
 *
 */
@Service
public class DepartmentService {
	@Autowired
	private DepartmentDao departmentDao;

	public static String deptKey() {
		return SysManager.getConfig().getTokenName() + ":department_key";
	}

	public List<Department> selectAll() {
		String departmentStr = SysManager.getRedis().get(deptKey());
		List<Department> allDeptList = JSONArray.parseArray(departmentStr, Department.class);
		if (allDeptList == null || allDeptList.isEmpty()) {
			allDeptList = departmentDao.selectAll();
			SysManager.getRedis().set(deptKey(), JSON.toJSONString(allDeptList));
		}
		return allDeptList;
	}

	public List<Department> selectAllCompany() {
		List<Department> companylList = new ArrayList<Department>();
		List<Department> allDeptList = selectAll();
		for (Department department : allDeptList) {
			if (department.getLevels() == 1) {
				companylList.add(department);
			}
		}
		return companylList;
	}

	public List<Department> selectAllChild(Integer parentId) {
		List<Department> deptList = new ArrayList<Department>();

		List<Department> allDeptList = selectAll();
		List<Department> list = DepartmentAlgorithm.findAllChild(parentId, allDeptList);
		if (list != null && !list.isEmpty()) {
			for (Department dept : list) {
				if (dept.getForService() == 1) {
					deptList.add(dept);
				}
			}
		}
		return deptList;
	}

	public Department select(Integer id) {
		return departmentDao.selectById(id, Department.class);
	}

	public boolean add(Department dept) {
		if (dept.getParentId() == null) {
			dept.setParentId(0);
		}
		dept.setDeleted(0);
		int result = departmentDao.insert(dept);
		if (result < 0) {
			throw new SystemException("操作失败");
		}

		// 更新redis
		List<Department> deptList = departmentDao.selectAll();
		if (deptList != null && !deptList.isEmpty()) {
			SysManager.getRedis().update(deptKey(), JSON.toJSONString(deptList));
		}

		return true;
	}

	public boolean update(Department dept) {
		Department deptInfo = departmentDao.selectById(dept.getId(), Department.class);
		if (deptInfo == null) {
			throw new SystemException("未查询到部门信息");
		}
		BeanUtils.copyObject(deptInfo, dept);
		int result = departmentDao.update(deptInfo);
		if (result < 0) {
			throw new SystemException("操作失败");
		}

		// 更新redis
		List<Department> deptList = departmentDao.selectAll();
		if (deptList != null && !deptList.isEmpty()) {
			SysManager.getRedis().update(deptKey(), JSON.toJSONString(deptList));
		}
		return true;
	}

	public boolean delete(Integer id) {
		Department deptInfo = departmentDao.selectById(id, Department.class);
		if (deptInfo == null) {
			throw new SystemException("未查询到部门信息");
		}
		int result = departmentDao.delete(new Department().setId(deptInfo.getId()));
		if (result < 0) {
			throw new SystemException("操作失败");
		}

		// 更新redis
		List<Department> deptList = departmentDao.selectAll();
		if (deptList != null && !deptList.isEmpty()) {
			SysManager.getRedis().update(deptKey(), JSON.toJSONString(deptList));
		}
		return true;
	}

}
